package com.rongmai.db.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.Duration;
/**
 * @author zhenhua.sun
 * @date 11/4/17
 */
public class DateUtils {

    public static String YYYY_MM ="yyyy-MM-dd";
    public static String YYYY_MM_DD_HH_MM_SS ="yyyy-MM-dd HH:mm:ss";
    
    public static Boolean isToday(Date date){
        if(date == null){
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Date today = calendar.getTime();
        return today.before(date);
    }

    public static Date parseDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("parse date failed.");
        }
    }
    
    public static Date parseDate(String dateStr, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("parse date failed.");
        }
    }

    /**
     * 分钟差
     */
    public static long diffMinutes(Date date) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();

        // 计算日期差值
        Duration duration = Duration.between(localDateTime, now);
        return duration.toMinutes();

    }
}
